/**
 * 六合彩的规则
 */
var lhcRule = function() {
    this.map = {
    };

    // 特码
    var arr = ['大', '小', '单', '双', '尾大', '尾小', '合大', '合小', '合单', '合双', '大单', '大双', '小单', '小双'];
    var arr2 = [1.98, 1.98, 1.98, 1.98, 1.98, 1.98,  1.98,   1.98,   1.98,   1.98,  3.7,    3.7,   3.7,     3.7];
    for (var i in arr) {
        this.map['特' + arr[i]] = {
            display: '特码 - ' + arr[i],
            rate: arr2[i],
        }
    }

    var arr = ['天肖', '地肖', '前肖', '后肖', '家肖', '野肖'];
    for (var i in arr) {
        this.map['特' + arr[i][0]] = {
            display: '特码 - ' + arr[i],
            rate: 1.98,
        }
    }

    var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
    for (var i in arr) {
        this.map['特' + arr[i]] = {
            display: '特码 - ' + arr[i],
            rate: i == 9 ? 9.5 : 11.5,
        }
    }

    // 五行
    var arr = ['金', '木', '水', '火', '土'];
    for (var i in arr)
        this.map['特' + arr[i]] = {
            display: '五行 - ' + arr[i],
            rate: i == 4 ? 5.2 : 4.78,
        }

    // 色波
    var arr = ['红', '蓝', '绿'];
    var arr2 = [2.7, 2.9, 2.9];
    for (var i in arr) {
        this.map['特' + arr[i]] = {
            display: arr[i] + '波',
            rate: arr2[i],
        }
    }

    // 半波
    var arr3 = ['单', '双', '大', '小'];
    var m = {
        '红': [5.4, 4.8, 6, 4.5],
        '蓝': [5.4, 5.4, 4.8, 6],
        '绿': [5.4, 6, 4.8, 6]
    };
    for (var i in arr) {
        for (var j in arr3) {
            this.map['特' + arr[i] + arr3[j]] = {
                display: '半波 - ' + arr[i] + arr3[j],
                rate: m[arr[i]][j],
            }
        }
    }

    // 半半波
    var arr3 = ['大单', '大双', '小单', '小双'];
    var m = {
        '红': [13.5, 10.5, 8, 8],
        '蓝': [8, 10.5, 13.5, 10.5],
        '绿': [10.5, 10.5, 10.5, 13.5]
    };
    for (var i in arr) {
        for (var j in arr3) {
            this.map['特' + arr[i] + arr3[j]] = {
                display: '半半波 - ' + arr[i] + arr3[j],
                rate: m[arr[i]][j],
            }
        }
    }

    // 特码头尾
    for (var i = 0; i <= 4; i++) {
        this.map["特头" + i] = {
            display: "特码" + i + "头",
            rate: i == 0 ? 5.1 : 4.6,
        }
    }
    for (var i = 0; i <= 9; i++) {
        this.map["特尾" + i] = {
            display: "特码" + i + "尾",
            rate: i == 0 ? 11.5 : 9.2,
        }
    }

    // 49个号码
    for (var i = 1; i <= 49; i++) {
        var k = i < 10 ? "0" + i : "" + i;
        this.map["特码" + k] = {
            display: '特码A - ' + k,
            rate: 43,
        }
        this.map["特码B" + k] = {
            display: '特码B - ' + k,
            rate: 48.5,
        }
    }

    // 正码
    for (var i = 1; i <= 49; i++) {
        var k = i;
        this.map["正" + k] = {
            display: "正码 - " + k,
            rate: 7.21,
        }
    }

    var arr = ['大', '小', '单', '双'];
    for (var i in arr) {
        this.map["和" + arr[i]] = {
            display: "正码总和 - " + arr[i],
            rate: 1.98,
        }
    }

    // 一肖、正肖
    var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
    for (var i in arr) {
        this.map["平特" + arr[i]] = {
            display: "一肖 - " + arr[i],
            rate: i == 9 ? 1.8 : 2.09,
        }
        this.map["正" + arr[i]] = {
            display: "正肖 - " + arr[i],
            rate: i == 9 ? 1.76 : 1.92,
        }
    }

    // 正一特 ~ 正六特
    var arr = ['一', '二', '三', '四', '五', '六'];
    var arr2 = ['大', '小', '单', '双', '合单', '合双', '合大', '合小', '红', '蓝', '绿', '尾大', '尾小'];
    var arr3 = [1.98, 1.98, 1.98, 1.98, 1.98, 1.98,    1.98,   1.98,   2.7,  2.9,  2.9, 1.98, 1.98];
    for (var i in arr) {
        // 49个号码
        for (var j = 1; j <= 49; j++) {
            var k = j < 10 ? "0" + j : "" + j;
            this.map[arr[i] + "码" + k] = {
                display: "正" + arr[i] + "特 - " + k,
                rate: 42,
            }
        }

        for (var j in arr2) {
            this.map[arr[i] + arr2[j]] = {
                display: "正" + arr[i] + "特 - " + arr2[j],
                rate: arr3[j],
            }
        }
    }

    // 合肖
    var arr = [5.6, 3.7, 2.8, 2.1, 1.97, 1.6, 1.4, 1.2, 1.12, 1.05];
    for (var i = 2; i <= 11; i++) {
        this.map["合" + i] = {
            display: '合肖 - ' + i + '个',
            rate: arr[i - 2],
        }
    }

    // 尾数
    for (var i = 0; i <= 9; i++) {
        this.map["平特尾" + i] = {
            display: "平特" + i + "尾",
            rate: i == 0 ? 2.09 : 1.8,
        }
    }

    // 7色波
    this.map["七红"] = {
        display: "7色波 - 红波",
        rate: 2.7,
    }
    this.map["七蓝"] = {
        display: "7色波 - 蓝波",
        rate: 2.9,
    }
    this.map["七绿"] = {
        display: "7色波 - 绿波",
        rate: 2.9,
    }
    this.map["七和"] = {
        display: "7色波 - 和局",
        rate: 23,
    }

    // 总肖
    for (var i = 2; i <= 7; i++) {
        var v = 14;
        if (i == 5) v = 3.06;
        if (i == 6) v = 1.97;
        if (i == 7) v = 5.3;
        this.map["总" + i] = {
            display: i + "肖",
            rate: v,
        }
    }
    this.map["总单"] = {
        display: "总肖 - 单",
        rate: 1.97,
    }
    this.map["总双"] = {
        display: "总肖 - 双",
        rate: 1.97,
    }

    // 不中
    var arr = [2.17, 2.63, 3.18, 3.72, 4.5, 5.58, 6.8, 8.5];
    for (var i = 5; i <= 12; i++) {
        this.map["不中" + i] = {
            display: "不中" + i,
            rate: arr[i - 5],
        }
    }

    this.map['和大'] = {
        display: '总和大',
        rate: 1.98,
    };
    this.map['和小'] = {
        display: '总和小',
        rate: 1.98,
    };
    this.map['和单'] = {
        display: '总和单',
        rate: 1.98,
    };
    this.map['和双'] = {
        display: '总和双',
        rate: 1.98,
    };

    var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
    for (var i in arr) {
        this.map["连肖2" + arr[i]] = {
            display: "二连肖" + arr[i],
            rate: i == 9 ? 3.32 : 4.12,
        }
        this.map["连肖3" + arr[i]] = {
            display: "三连肖" + arr[i],
            rate: i == 9 ? 9.02 : 11.12,
        }
        this.map["连肖4" + arr[i]] = {
            display: "四连肖" + arr[i],
            rate: i == 9 ? 25.3 : 31.2,
        }
        this.map["连肖5" + arr[i]] = {
            display: "五连肖" + arr[i],
            rate: i == 9 ? 87 : 98,
        }
    }

    for (var i = 0; i <= 9; i++) {
        this.map["连尾2" + i] = {
            display: "三连尾" + i,
            rate: 3.18,
        }
        this.map["连尾3" + i] = {
            display: "三连尾" + i,
            rate: 7.08,
        }
        this.map["连尾4" + i] = {
            display: "四连尾" + i,
            rate: 15.8,
        }
        this.map["连尾5" + i] = {
            display: "五连尾" + i,
            rate: i == 0 ? 45 : 40,
        }
    }

    this.map["中3"] = {
        display: "三中二",
        rate: 20,
    }
    this.map["三中三"] = {
        display: "三中二之三中三",
        rate: 100,
    }
    this.map["全3"] = {
        display: "三全中",
        rate: 670,
    }
    this.map["全2"] = {
        display: "二全中",
        rate: 60,
    }
    this.map["中2"] = {
        display: "二中特",
        rate: 25.5,
    }
    this.map["二中特"] = {
        display: "二中特",
        rate: 50,
    }
    this.map["特串"] = {
        display: "特串",
        rate: 150,
    }
    this.map["全4"] = {
        display: "四全中",
        rate: 950,
    }
};
lhcRule.prototype = {};

// 计算总和
lhcRule.prototype.he = function(list) {
    var total = 0;
    for (var i in list) {
        total += parseInt(list[i]);
    }
    return total;
}

lhcRule.prototype.heOdd = function(list) {
    var total = this.he(list);
    return total % 2 == 1;
}
lhcRule.prototype.heBig = function(list) {
    var total = this.he(list);
    return total >= 175;
}

lhcRule.prototype.qiSeBo = function(list) {
    var hong = 0, lan = 0, lv = 0;
    for (var i = 0; i < 6; i++) {
        var n = parseInt(list[i]);
        if (this.hongbo().indexOf(n) != -1) hong++;
        else if (this.lanbo().indexOf(n) != -1) lan++;
        else if (this.lvbo().indexOf(n) != -1) lv++;
    }

    {
        var n = parseInt(list[6]);
        if (this.hongbo().indexOf(n) != -1) hong += 1.5;
        else if (this.lanbo().indexOf(n) != -1) lan += 1.5;
        else if (this.lvbo().indexOf(n) != -1) lv += 1.5;
    }
    if (hong > lan && hong > lv) return '红';
    if (lan > hong && lan > lv) return '蓝';
    if (lv > hong && lv > lan) return '绿';
    return '和';
}

// 取得色波号码
lhcRule.prototype.hongbo = function() {
    return [1, 2, 7, 8, 12, 13, 18, 19, 23, 24, 29, 30, 34, 35, 40, 45, 46];
}
lhcRule.prototype.lanbo = function() {
    return [3, 4, 9, 10, 14, 15, 20, 25, 26, 31, 36, 37, 41, 42, 47, 48];
}
lhcRule.prototype.lvbo = function() {
    return [5, 6, 11, 16, 17, 21, 22, 27, 28, 32, 33, 38, 39, 43, 44, 49];
}

// 五行
lhcRule.prototype.jin = function() {
    return [1, 6, 11, 16, 21, 26, 31, 36, 41, 46];
}
lhcRule.prototype.mu = function() {
    return [2, 7, 12, 17, 22, 27, 32, 37, 42, 47];
}
lhcRule.prototype.shui = function() {
    return [3, 8, 13, 18, 23, 28, 33, 38, 43, 48];
}
lhcRule.prototype.huo = function() {
    return [4, 9, 14, 19, 24, 29, 34, 39, 44, 49];
}
lhcRule.prototype.tu = function() {
    return [5, 10, 15, 20, 25, 30, 35, 40, 45];
}

// 取得号码对应的生肖（注意：每年都需要修改哦！）
lhcRule.prototype.getShengXiao = function(n) {
    var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
    var oneIs = '鸡'; // 1号属鸡
    var index = arr.indexOf(oneIs);
    n = (n - 1) % 12;

    return arr[(index - n + 12) % 12];
}

// 取得某个生肖的号码
lhcRule.prototype.getXiaoNumbers = function(xiao) {
    var self = this;
    var list = [];
    for (var i = 1; i <= 49; i++) {
        if (self.getShengXiao(i) == xiao) list.push(i);
    }
    return list;
}

/**
 * 计算长龙信息
 */
lhcRule.prototype.long = function(list) {
    var self = this;
    var r = [];
    if (isEmpty(list)) return r;

    // 总和: 大、小
    var l0 = list[0].red;

    var f = function(k, cb) {
        var t = 1;
        for (var i = 1; i < list.length; i++) {
            var l = list[i].red;
            if (!cb(l)) break;
            t++;
        }
        var oo = {}; oo[k] = t;
        if (t > 2) r.push(oo);
    };

    // 总和单双、大小、七色波
    if (self.heOdd()) {
        f("总和 - 单", function(l) {
            return self.heOdd(l);
        });
    }
    else {
        f("总和 - 双", function(l) {
            return !self.heOdd(l);
        });
    }
    if (self.heBig(l0)) {
        f("总和 - 大", function(l) {
            return self.heBig(l);
        });
    }
    else {
        f("总和 - 小", function(l) {
            return !self.heBig(l);
        });
    }

    // 七色波
    var bo = self.qiSeBo(l0);
    if (bo == '红') {
        f("七色波 - 红", function(l) {
            return self.qiSeBo(l) == '红';
        });
    }
    else if (bo == '蓝') {
        f("七色波 - 蓝", function(l) {
            return self.qiSeBo(l) == '蓝';
        });
    }
    else if (bo == '绿') {
        f("七色波 - 绿", function(l) {
            return self.qiSeBo(l) == '绿';
        });
    }
    else if (bo == '和') {
        f("七色波 - 和", function(l) {
            return self.qiSeBo(l) == '和';
        });
    }

    // 特码的大小单双等
    var te = parseInt(l0[6]);
    if (te % 2 == 0) {
        f("特码 - 双", function(l) {
            return parseInt(l[6]) % 2 == 0;
        });
    }
    else {
        f("特码 - 单", function(l) {
            return parseInt(l[6]) % 2 == 1;
        });
    }
    if (te >= 25) {
        f("特码 - 大", function(l) {
            return parseInt(l[6]) >= 25;
        });
    }
    else {
        f("特码 - 小", function(l) {
            return parseInt(l[6]) < 25;
        });
    }
    var he = Math.floor(te /10) + (te % 10);
    if (he % 2 == 0) {
        f("特码 - 合数双", function(l) {
            var n2 = parseInt(l[6]);
            var h2 = Math.floor(n2 /10) + (n2 % 10);
            return h2 % 2 == 0;
        });
    }
    else {
        f("特码 - 合数单", function(l) {
            var n2 = parseInt(l[6]);
            var h2 = Math.floor(n2 /10) + (n2 % 10);
            return h2 % 2 == 1;
        });
    }
    if (he >= 7) {
        f("特码 - 合数大", function(l) {
            var n2 = parseInt(l[6]);
            var h2 = Math.floor(n2 /10) + (n2 % 10);
            return h2 >= 7;
        });
    }
    else {
        f("特码 - 合数小", function(l) {
            var n2 = parseInt(l[6]);
            var h2 = Math.floor(n2 /10) + (n2 % 10);
            return h2 < 7;
        });
    }
    var wei = te % 10;
    if (wei >= 5) {
        f("特码 - 尾数大", function(l) {
            var n2 = parseInt(l[6]);
            var h2 = n2 % 10;
            return h2 >= 5;
        });
    }
    else {
        f("特码 - 尾数小", function(l) {
            var n2 = parseInt(l[6]);
            var h2 = n2 % 10;
            return h2 < 5;
        });
    }

    // 排序
    r.sort(function(a, b) {
        var v1, v2;
        for (var k in a) v1 = a[k];
        for (var k in b) v2 = b[k];
        return v2 - v1;
    });
    return r;
}

/**
 * 转换显示的名称
 */
lhcRule.prototype.getTargetName = function(name) {
    var lianxiaoMap = {
        '连肖2': '二连肖',
        '连肖3': '三连肖',
        "连肖4": '四连肖',
        "连肖5": '五连肖',
        "连尾2": '二连尾',
        "连尾3": '三连尾',
        "连尾4": '四连尾',
        "连尾5": '五连尾'
    };

    var arr = name.split(":");
    if (arr.length == 1) {
        if (lianxiaoMap[name]) return lianxiaoMap[name];
        if (!this.map[name]) return name;
        return this.map[name].display;
    }

    if (lianxiaoMap[arr[0]])
        return lianxiaoMap[arr[0]] + " &nbsp;<span class='blue' style='font-size:12px;'>" + arr[1] + "</span>";
    return this.map[arr[0]].display + " &nbsp;<span class='blue' style='font-size:12px;'>" + arr[1] + "</span>";
};

window.LhcRule = new lhcRule();
